<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Cities;
use App\Models\PartMaster;
use App\Models\MacroManagement;
use App\Models\MakeMaster;
use App\Models\ModelMaster;
use App\Models\FilmTypeMaster;
use App\Models\ProductTypes;
use App\Models\ProvinceMaster;
use App\Models\RegionMaster;
use App\Services\ModuleService;
use Encore\Admin\Grid\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SettingPartController extends Controller
{
    public function index(Request $request)
    {
        checkAuthority();
        $page = [
            'page_name' => 'CXBW',
            'title' => ts('CXBW'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = DB::table('part_master')
            ->whereNull('deleted_at');
        $product_type_id = $request->input('product_type_id');
        if ($product_type_id) {
            $model = $model->where('product_type_id', $product_type_id);
        }
        $model = filterByColumns($model, ['english_value', 'traditional_chiness_value', 'simplified_chiness_value']);

        $data = $model->orderByDesc('id')
            ->paginate(50);

        $menus = (new ModuleService())->getWebButton(session('admin.role_id') ?? 0, $request->path());

        return view('admin.part.index', [
            'data'=> $data,
            'page' => $page,
            'menus' => $menus,
        ]);
    }

    public function create(Request $request)
    {
        checkAuthority();
        $page = [
            'page_name' => 'CXBW',
            'title' => ts('addnew'),
            'breadcrumb' => [
                [
                    'text' => ts('CXBW'),
                    'url' => '/admin/setting/part/'
                ]
            ],
        ];
        $data = new PartMaster();
        return view('admin.part.create', [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function store(Request $request)
    {
        checkAuthority();
        $model = new PartMaster();
        $model->product_type_id = (int) $request->input('product_type_id') ?: 1;
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));

        $model->min_length = (int) $request->input('min_length') ?: 1;
        $model->order = (int) $request->input('order');
        $model->save();
        makeLangFiles();
        return redirect('/admin/setting/part')->with('trash', '新增成功！');
    }

    public function edit(Request $request, $id)
    {
        checkAuthority();
        $page = [
            'page_name' => 'CXBW',
            'title' => ts('edit'),
            'breadcrumb' => [
                [
                    'text' => ts('CXBW'),
                    'url' => '/admin/setting/part/'
                ]
            ],
        ];
        $data = PartMaster::findOrFail($id);
        return view('admin.part.edit', [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function update(Request $request, $id)
    {
        checkAuthority();
        $model = PartMaster::findOrFail($id);
        $model->product_type_id = (int) $request->input('product_type_id') ?: 1;
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->min_length = (int) $request->input('min_length') ?: 1;
        $model->order = (int) $request->input('order');
        $model->save();
        makeLangFiles();
        return redirect('/admin/setting/part')->with('trash', '更新成功！');
    }


    public function destroy(Request $request, $id)
    {
        $model = PartMaster::findOrFail($id);
        $model->delete();
        return fs200('deleteSuccess');
    }
}
